package cn.net.isir.controller.miniprogram;

import com.github.pagehelper.PageInfo;
import cn.net.isir.common.RequestObject;
import cn.net.isir.common.ResultObject;
import cn.net.isir.entity.Category;
import cn.net.isir.entity.Goods;
import cn.net.isir.service2.CategoryService;
import cn.net.isir.service2.GoodsService;
import cn.net.isir.service2.GoodsSpecificationService;
import cn.net.isir.utils.RequestUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxubo
 * @filename CategoryController.java
 * @package cn.net.isir.controller.miniprogram
 * @email lxb@isir.net.cn
 * @date 2018/11/12
 * @comment
 * @since 1.8
 */
@RestController("minipCategoryController")
@RequestMapping(value = "/minip/category")
@Transactional
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsSpecificationService goodsSpecificationService;

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public ResultObject getCategoryByShopid(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        Category category = new Category();
        category.setShopId(shopid);
        List<Category> categoryList = categoryService.selectList(category);
        ro.setResult(200);
        ro.setMessage("success");
        ro.setObject(categoryList);
        return ro;
    }

    /**
     * 支付宝专用的菜品列表
     *
     * @param obj      参数
     * @param request  请求
     * @param response 返回
     * @return 结果
     */
    @RequestMapping(method = RequestMethod.GET, value = "/listwg")
    public ResultObject getCategoryWithGoodsByShopid(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid", "goodsPageSize"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        String goodsPageSizeStr = String.valueOf(reqParamMap.get("goodsPageSize"));
        Integer goodsPageSize = Integer.parseInt(goodsPageSizeStr);
        Category category = new Category();
        category.setShopId(shopid);
        List<Category> categoryList = categoryService.selectList(category);
        //组织返回数据
        for (Category c : categoryList) {
            Goods g = new Goods();
            g.setCategoryId(c.getId());
            g.setSaleStatus("onsale");
            g.setShopId(shopid);//增加店铺选项
            PageInfo<Goods> pageInfo = goodsService.selectPageList(g, 1, goodsPageSize);//固定查询十个，因为每页个数固定了
            if (pageInfo.getList().size() > 0) {
                c.setGoodsPage(pageInfo);
            }
        }

        ro.setResult(200);
        ro.setMessage("success");
        ro.setObject(categoryList);
        return ro;
    }
}
